<template>
	<div id="nav">
		<!-- router-link 用于切换路由 -->
		<div class="tabtitle"
		:class="{'selected':curIndex==0}"
		@click="setIndex(0,'/')">
			<div class="txt">
				推荐音乐
			</div>
		</div> |
		<div class="tabtitle"
		:class="{'selected':curIndex==1}"
		@click="setIndex(1,'hotsong')">
			<div class="txt">
				热歌榜
			</div>
		</div> |
		<div class="tabtitle"
		:class="{'selected':curIndex==2}"
		@click="setIndex(2,'search')">
			<div class="txt">
				搜索
			</div>
		</div>
	</div>
</template>

<script>
	// 定义路由列表
	const routerList = ["/","/hotsong","/rearch"];
	export default {
		created() {
			// 更新curIndex
			this.updateIndex()
		},
		data(){
			return {
				curIndex:0
			}
		},
		methods:{
			updateIndex(){
				// 获取当前路由地址
				// console.log(this.$route.path);
				let nowIndex=routerList.indexOf(this.$route.path);
				this.curIndex = nowIndex>0?nowIndex:0;
			},
			setIndex(i,path){
				// console.log(i);
				this.curIndex=i;
				// 编程式导航
				this.$router.push({
					path:path
				})
			}
		}
	}
</script>

<style lang="scss">
	#nav {
		// position: fixed;
		top: 84px;
		left: 0;
		z-index: 100;
		width: 100%;

		display: flex;
		justify-content: center;
		align-items: center;
		height: 40px;
		background-color: #fff;
	}

	.tabtitle {
		-webkit-flex: 1;
		flex: 1;
		display: block;
		width: 30%;
		height: 100%;
		text-align: center;
		cursor: pointer;
	}

	.txt {
		position: relative;
		display: inline-block;
		max-width: 100%;
		height: 100%;
		padding: 0 5px;
		box-sizing: border-box;
		color: #333;
		font-size: 15px;
		line-height: 40px;


	}

	.selected .txt {
		color: #dd001b;
	}

	.selected .txt::after {
		content: "";
		position: absolute;
		left: 0;
		bottom: -1px;
		z-index: 3;
		width: 100%;
		height: 4px;
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background: #dd001b;
	}
</style>
